package com.sgcc.pda.mdrh.ui.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;

import com.sgcc.pda.mdrh.task.safelibrary.R;

/**
 * 播放视频
 */
public class VideoPlayActivity extends AppCompatActivity {


    private VideoView mVideoView;
//    private String url5 = "http://192.168.11.135:19000/weblogic/app/eomdoc/20201020/995eacf8351f4ff1885768c8fe300a89.mp4";
//    private String url5 = "http://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218114723HDu3hhxqIT.mp4";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        initView();
    }

    protected int getLayoutId() {
        return R.layout.activity_video_play;
    }

    protected void initView() {
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 隐藏android系统的状态栏

        mVideoView = findViewById(R.id.videoview);
        initVideoView();
        mVideoView.start();
    }

    private void initVideoView() {

        Intent intent = getIntent();
        String url = intent.getStringExtra("url");

        mVideoView.setVideoPath(url);//设置视频文件
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                //视频加载完成,准备好播放视频的回调

            }
        });
        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                //视频播放完成后的回调

            }
        });
        mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                //异常回调
                return false;//如果方法处理了错误，则为true；否则为false。返回false或根本没有OnErrorListener，将导致调用OnCompletionListener。
            }
        });
//        mVideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
//            @Override
//            public boolean onInfo(MediaPlayer mp, int what, int extra) {
//                //信息回调
////                what 对应返回的值如下
////                public static final int MEDIA_INFO_UNKNOWN = 1;  媒体信息未知
////                public static final int MEDIA_INFO_VIDEO_TRACK_LAGGING = 700; 媒体信息视频跟踪滞后
////                public static final int MEDIA_INFO_VIDEO_RENDERING_START = 3; 媒体信息\视频渲染\开始
////                public static final int MEDIA_INFO_BUFFERING_START = 701; 媒体信息缓冲启动
////                public static final int MEDIA_INFO_BUFFERING_END = 702; 媒体信息缓冲结束
////                public static final int MEDIA_INFO_NETWORK_BANDWIDTH = 703; 媒体信息网络带宽（703）
////                public static final int MEDIA_INFO_BAD_INTERLEAVING = 800; 媒体-信息-坏-交错
////                public static final int MEDIA_INFO_NOT_SEEKABLE = 801; 媒体信息找不到
////                public static final int MEDIA_INFO_METADATA_UPDATE = 802; 媒体信息元数据更新
////                public static final int MEDIA_INFO_UNSUPPORTED_SUBTITLE = 901; 媒体信息不支持字幕
////                public static final int MEDIA_INFO_SUBTITLE_TIMED_OUT = 902; 媒体信息字幕超时
//
//                return false; //如果方法处理了信息，则为true；如果没有，则为false。返回false或根本没有OnInfoListener，将导致丢弃该信息。
//            }
//        });

        mVideoView.setMediaController(new MediaController(this));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mVideoView != null) {
            mVideoView.stopPlayback();//停止播放视频,并且释放
            mVideoView.suspend();//在任何状态下释放媒体播放器
        }
    }


}
